import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

Window {
    id: rootWindow
    width: 600
    height: 800
    visible: true
    title: "聊天室"

    // 登录状态
    property bool isLoggedIn: false
    property string username: ""

    // 登录对话框
    Dialog {
        id: loginDialog
        title: "登录聊天室"
        anchors.centerIn: parent
        width: 300
        modal: true

        ColumnLayout {
            anchors.fill: parent
            spacing: 10

            TextField {
                id: loginUsername
                placeholderText: "用户名"
                Layout.fillWidth: true
            }

            TextField {
                id: loginPassword
                placeholderText: "密码"
                echoMode: TextInput.Password
                Layout.fillWidth: true
            }

            RowLayout {
                Layout.alignment: Qt.AlignRight

                Button {
                    text: "登录"
                    onClicked: {
                        if (chatClient.login(loginUsername.text, loginPassword.text)) {
                            rootWindow.username = loginUsername.text
                            rootWindow.isLoggedIn = true
                            loginDialog.close()
                        } else {
                            loginErrorDialog.open()
                        }
                    }
                }

                Button {
                    text: "取消"
                    onClicked: Qt.quit()
                }
            }
        }
    }

    // 登录错误对话框
    Dialog {
        id: loginErrorDialog
        title: "登录失败"
        anchors.centerIn: parent
        Label {
            text: "用户名或密码不正确"
        }
    }

    // 密码修改对话框
    Dialog {
        id: changePasswordDialog
        title: "修改密码"
        anchors.centerIn: parent
        width: 300
        modal: true

        ColumnLayout {
            anchors.fill: parent
            spacing: 10

            TextField {
                id: oldPassword
                placeholderText: "旧密码"
                echoMode: TextInput.Password
                Layout.fillWidth: true
            }

            TextField {
                id: newPassword
                placeholderText: "新密码"
                echoMode: TextInput.Password
                Layout.fillWidth: true
            }

            TextField {
                id: confirmPassword
                placeholderText: "确认新密码"
                echoMode: TextInput.Password
                Layout.fillWidth: true
            }

            RowLayout {
                Layout.alignment: Qt.AlignRight

                Button {
                    text: "修改"
                    onClicked: {
                        if (newPassword.text !== confirmPassword.text) {
                            passwordMismatchDialog.open()
                            return
                        }

                        if (chatClient.changePassword(oldPassword.text, newPassword.text)) {
                            passwordChangedDialog.open()
                            changePasswordDialog.close()
                        } else {
                            passwordChangeFailedDialog.open()
                        }
                    }
                }

                Button {
                    text: "取消"
                    onClicked: changePasswordDialog.close()
                }
            }
        }
    }

    // 密码不匹配对话框
    Dialog {
        id: passwordMismatchDialog
        title: "密码错误"
        anchors.centerIn: parent
        Label {
            text: "新密码输入不一致"
        }
    }

    // 密码修改成功对话框
    Dialog {
        id: passwordChangedDialog
        title: "修改成功"
        anchors.centerIn: parent
        Label {
            text: "密码已成功修改"
        }
    }

    // 密码修改失败对话框
    Dialog {
        id: passwordChangeFailedDialog
        title: "修改失败"
        anchors.centerIn: parent
        Label {
            text: "密码修改失败，请检查旧密码"
        }
    }

    // 主内容区域
    ColumnLayout {
        anchors.fill: parent
        spacing: 0

        // 顶部工具栏
        Rectangle {
            Layout.fillWidth: true
            height: 40
            color: "#e0e0e0"

            // 内边距容器
            Item {
                anchors.fill: parent
                anchors.margins: 10

                Row {
                    anchors.fill: parent
                    spacing: 10

                    Label {
                        text: rootWindow.isLoggedIn ? `用户: ${rootWindow.username}` : "未登录"
                        font.bold: true
                        verticalAlignment: Text.AlignVCenter
                        height: parent.height
                    }

                    Button {
                        text: "修改密码"
                        onClicked: changePasswordDialog.open()
                        enabled: rootWindow.isLoggedIn
                        height: parent.height
                    }
                }
            }
        }

        // 聊天消息显示区域
        ListView {
            id: messageView
            Layout.fillWidth: true
            Layout.fillHeight: true
            clip: true
            spacing: 5
            model: messageModel

            delegate: Rectangle {
                width: messageView.width - 20
                height: messageContent.height + 10
                color: model.isSystem ? "#f0f8ff" : "#f5f5f5"
                radius: 5

                Text {
                    id: messageContent
                    width: parent.width - 20
                    wrapMode: Text.Wrap
                    textFormat: Text.RichText
                    padding: 5
                    text: model.message
                    font.pixelSize: 14
                }
            }

            // 自动滚动到底部
            onCountChanged: {
                messageView.positionViewAtEnd()
            }
        }

        // 消息输入区域
        Rectangle {
            id: inputArea
            height: 80
            Layout.fillWidth: true
            color: "#e0e0e0"
            radius: 5

            // 内边距容器
            Item {
                anchors.fill: parent
                anchors.margins: 5

                Row {
                    anchors.fill: parent
                    spacing: 5

                    TextField {
                        id: inputField
                        width: parent.width - 90
                        height: parent.height
                        placeholderText: "输入消息..."
                        font.pixelSize: 16
                        enabled: rootWindow.isLoggedIn
                        onAccepted: sendButton.clicked()
                    }

                    Button {
                        id: sendButton
                        text: "发送"
                        width: 80
                        height: parent.height
                        enabled: rootWindow.isLoggedIn
                        onClicked: {
                            if (inputField.text) {
                                chatClient.sendMessage(inputField.text)
                                inputField.clear()
                            }
                        }
                    }
                }
            }
        }
    }

    // 聊天消息模型
    ListModel {
        id: messageModel
    }

    // 添加消息到显示区域
    function appendMessage(msg) {
        var isSystem = msg.startsWith("系统消息:") || msg.startsWith("连接错误:");
        messageModel.append({
            message: `<b>[${new Date().toLocaleTimeString(Qt.locale(), "hh:mm:ss")}]</b> ${msg}`,
            isSystem: isSystem
        });
    }

    // 初始化时打开登录对话框
    Component.onCompleted: {
        if (!isLoggedIn) {
            loginDialog.open()
        }
    }
}
